<?php
class WebsiteController extends  IndexController
{
    public function init()
    {
		parent::init();
        $this->utl            = Yaf_Registry::get("utl");
        $this->WebsiteModel   = new WebsiteModel();
        $this->UserLogsModel  = new LogsModel();
        $this->Session        = Yaf_Registry::get("session");
    }

    public function listAction()
    {
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $sites_info        = $this->WebsiteModel->getSitesInfo($startLimit,$offset);
        $allCounts         = $this->WebsiteModel->getAllCount();
        $allpage           = $this->utl::page('/Website/list', $page, $allCounts[0]['rows'], $offset, $status='');
        $sites_list        = array();
        foreach ($sites_info AS $k => $v)
        {
            $sites_list[$k]                 = $v;
            $sites_list[$k]['current_time'] = date('Y-m-d H:i:s', (int) $v['create_time']);
            $sites_list[$k]['rows']         = $allCounts[0]['rows'];
            $sites_list[$k]['every_page']   = $offset;
        }
        $this->getView()->assign("session_user",$this->Session->user_info);
        $this->getView()->assign("sites_list",$sites_list);
        $this->getView()->assign("allpage",$allpage);
    }

    public function pageAction()
    {
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $sites_info        = $this->WebsiteModel->getSitesInfo($startLimit,$offset);
        $allCounts         = $this->WebsiteModel->getAllCount();
        $allpage           = $this->utl::page('/Website/list', $page, $allCounts[0]['rows'], $offset, $status='');
        $sites_list        = array();
        foreach ($sites_info AS $k => $v)
        {
            $sites_list[$k] = $v;
            $sites_list[$k]['current_time'] = date('Y-m-d H:i:s', (int) $v['create_time']);
            $sites_list[$k]['rows']         = $allCounts[0]['rows'];
            $sites_list[$k]['every_page']   = $offset;
        }
        $this->getView()->assign("session_user",$this->Session->user_info);
        $this->getView()->assign("sites_list",$sites_list);
        exit(json_encode($sites_list, true));
    }

    public function currentPageAction()
    {
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $sites_info        = $this->WebsiteModel->getSitesInfo($startLimit,$offset);
        $allCounts         = $this->WebsiteModel->getAllCount();
        $allpage           = $this->utl::page('/Website/list', $page, $allCounts[0]['rows'], $offset, $status='');
        exit($allpage);
    }

    public function delAction()
    {
        $id = $this->getRequest()->getParam('id');
        if (empty($id))
        {
            $this->forward('Website', 'article');
            return;
        }
        $rs = $this->WebsiteModel->del($id);
        $session_del = array();
        $user_info = $this->Session->user_info;
        foreach ($user_info as $k => $v) 
        {
            $session_del[$k]['user_id'] = $v['user_id'];
            $session_del[$k]['table_name'] = "website";
            $session_del[$k]['action'] = "del";
        }
        $this->UserLogsModel->addUser($session_del);
        header('Content-type:application/json;charset:utf8');
        if (false === $rs)
        {
            exit('{"status":false,"info":"删除失败！"}');
            return;
        }
        exit('{"status":true,"info":"删除成功"}');
        return true;
    }

    public function editAction()
    {
        $id = $this->getRequest()->getParam('id');
        if (empty($id))
        {
            $this->redirect('Website', 'article');
            return;
        }
        if ($this->getRequest()->isPost())
        {
            $post = $this->getRequest()->getPost();
            header('Content-type:application/json;charset:utf8');
            $rs = $this->WebsiteModel->edit($post, $id);
            $session_edit = array();
            $user_info = $this->Session->user_info;
            foreach ($user_info as $k => $v) 
            {
                $session_edit[$k]['user_id'] = $v['user_id'];
                $session_edit[$k]['table_name'] = "website";
                $session_edit[$k]['action'] = "edit";
            }
            $this->UserLogsModel->addUser($session_edit);
            if (false === $rs)
            {
                 exit('{"status":false,"info":"编辑失败！"}');
                 return;
            }
            exit('{"status":true,"info":"编辑成功！"}');
        }
    }

    public function addAction()
    {
        if ($this->getRequest()->isPost()) {
            $post = $this->getRequest()->getPost();
            header('Content-type:application/json;charset:utf8');
            $rs = $this->WebsiteModel->add($post);
            $session_add = array();
            $user_info = $this->Session->user_info;
            foreach ($user_info as $k => $v) 
            {
                $session_add[$k]['user_id'] = $v['user_id'];
                $session_add[$k]['table_name'] = "website";
                $session_add[$k]['action'] = "add";
            }
            $this->UserLogsModel->addUser($session_add);
            if (false === $rs)
            {
                exit('{"status":false,"info":"添加失败！"}');
                return;
            }
            exit('{"status":true,"info":"添加成功！"}');
        }
    }
}
?>